<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page import = "com.db4o.servlet.Db4oServlet" %>
<%@page import = "facades.*" %>
<%@page import = "java.util.*" %>
<%@page import = "auser.*" %>
<%@page import = "com.db4o.query.*" %>
<% SessionFacade sfacade = new SessionFacade(session); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link rel="stylesheet"  href="<%=request.getContextPath()%>/css/printpreview.css" type="text/css" media="screen" />
	<link rel="stylesheet"  href="<%=request.getContextPath()%>/css/print.css" type="text/css" media="print" />
	<script src="<%=request.getContextPath()%>/js/jquery.js"></script>
	<title>Stampa Di Bilancio</title>
	<style>
		body{width:17cm;}
		table{border-collapse: collapse;}
		th,td{vertical-align:top;margin:0;padding:2px 3px 1px 3px}
		table,th ,td,tbody#grid td{border:1px solid black}
		thead th{border:0;border-bottom:1px solid black}
	</style>
 
</head>
<body>
<% 
	  
	ParametersPDoppia paramPdoppia =  sfacade.getParametersPdoppia();
	int sqlCount = 0;
	java.util.List<com.spaceprogram.db4o.sql.Result> results=null;
	Utente utente = (Utente)session.getAttribute("userBean");
	String query=null;
	int annoCompetenza =2007;
	//VoceDiPrimaNotaDaScrittura v; v.getVocePianoDeiConti().getL1()
	if(request.getParameter("annoCompetenza")==null) annoCompetenza =2007;
	else annoCompetenza = new Integer(request.getParameter("annoCompetenza"));  
	pageContext.setAttribute("annoCompetenza",annoCompetenza);
	// eco "52,60,61,70,74,80,81,83,84,91"
	// patr "01,02,04,05,15,27,28,29,31,98,99"
	String strEconomico = paramPdoppia.getContiEconomico();
	String strPatrimoniale ="" ;
		
	int printCount =0;
	String[] curGroup=null;
	String where1;
	String where2;
	String tipo = request.getParameter("tipo");
	
	if(tipo==null) tipo="";
	if(!tipo.equals("")) where2=" AND v.vocePianoDeiConti.l2='" + tipo + "' " ;	
	else where2="";
%>  

<c:set var="livello" value="${empty param.level?'3':param.level}" /> 	
<c:set var="tipo" value="${empty param.tipo?'':param.tipo}" /> 	

<c:forEach begin="1" end="2" varStatus="st">

<%
	printCount++;
	
	if(printCount==1)   curGroup = paramPdoppia.getContiPatrimoniale().split(","); //curGroup=patrimoniale;
	else 				curGroup = paramPdoppia.getContiEconomico().split(",");
	
	where1="";
	
	for(int k=0; k<curGroup.length;k++ ){
		if(k==0)
			where1+=" v.vocePianoDeiConti.l1='" + curGroup[k] + "' " ;	
		else
			where1+=" OR v.vocePianoDeiConti.l1='" + curGroup[k] + "' " ;	
	}
	
	try{	
		query = 	"select * from  auser.VoceDiPrimaNotaDaScrittura v " + 
		" where " + 
		" v.circolo.codice='" +  utente.getCircolo().getCodice() + "'" +
		" AND v.parent.annoCompetenza=  " + annoCompetenza +
		where2 +
		" AND (" + where1 + ")" +
		" order by codice,dataScrittura  ";
		
		results = com.spaceprogram.db4o.sql.Sql4o.execute(sfacade.getContainer(), query);
		//out.print(query);
	}catch(Exception e){ 
		out.print(e.getMessage());	
		 
	}
	
%>


<table cellpadding="0" cellspacing="0" border="0" style="border:0px solid black;width:16cm">
	<thead> 	
	 	<tr><th colspan="6" style=";font-size:15pt;text-align:left;height:60px;vertical-align: middle;">
	 			<img style="float:right" src="<%=request.getContextPath() %>/img/logo.jpg"/>
	 			<b>
	 				<c:if test="${st.count==2}"><font style="font-size:10pt;font-weight:normal">segue:&nbsp;</font> </c:if>
	 				BILANCIO ANNO ${annoCompetenza}
	 				<c:if test="${st.count==1}"><br>Circolo ${userBean.circolo.descrizione}&nbsp;</c:if>
	 			</b>  
	</thead> 
	
	<tbody id="grid">
		<c:set var="g1" value="" />
		<c:set var="g2" value="" /> 
		
		<c:if test="${st.first}">
			<tr class='noprint' align="left"><th colspan="6">
				<form name="search" id="search" method="get" action="prn_bilancio.jsp">
					<a href='<%=request.getContextPath() %>/app/start/index.jsp'>Chiudi</a> 
					| <a href='javascript:print()'>Stampa</a> 
					&nbsp;Anno 	<select name="annoCompetenza" id="annoCompetenza">
									<option value="2007">2007
									<option value="2008">2008 
									<option value="2009">2009 
									<option value="2010">2010 
									<option value="2011">2011 
									<option value="2012">2012 
								</select> 
								<script>
									$("#annoCompetenza").val("${annoCompetenza}")
								</script>
					
					&nbsp;Livello <select name="level" id="level">
									<option value="1">Livello 1
									<option value="2">Livello 2
									<option value="3">Livello 3
									<option value="4">Livello 4
								</select> 
								<script>
									$("#level").val("${livello}")
								</script>
					
					
					&nbsp;Tipo <select name="tipo" id="tipo">
									<option value="">Tutto
									<option value="AU">AU
									<option value="FA">FA
									<option value="PA">PA
									<option value="UP">UP
								</select> 
								<script>
									$("#tipo").val("${tipo}")
								</script>
					
								 
					<input type="submit" value="Carica">
				</form>			
		
		</c:if>
				
		<c:set var="totdare" value="${0}" />
		<c:set var="totavere" value="${0}" />
		<c:set var="vocedelpatrimoniale" >
			del Patrimoniale
		</c:set>
		
		<c:set var="vocedelleconomico" >
			dell' Economico
		</c:set>
		
		<tr style="font-weight: bold"><td width="75">Codici&nbsp; 
				<td width="300">Voci&nbsp;${st.first?vocedelpatrimoniale:vocedelleconomico}
				<td width="60" align="right">Dare
				<td width="60" align="right">Avere
		
		<c:set var="g1" value="<%=new VoceDiPrimaNotaDaScrittura() %>" />				 		
		<c:forEach items="<%=results%>" var="obj" varStatus="status">
			<c:set var="mov" value="<%= ((com.spaceprogram.db4o.sql.Result)pageContext.getAttribute("obj")).getBaseObject(0) %>" />
			
			<c:set var="mov_codice" >
				${mov.vocePianoDeiConti.l1}.${livello>1?mov.vocePianoDeiConti.l2:'0'}.${livello>2?mov.vocePianoDeiConti.l3:'0'}.${livello>3?mov.vocePianoDeiConti.l4:'0'}
			</c:set> 
			<c:set var="g1_codice" >
				${g1.vocePianoDeiConti.l1}.${livello>1?g1.vocePianoDeiConti.l2:'0'}.${livello>2?g1.vocePianoDeiConti.l3:'0'}.${livello>3?g1.vocePianoDeiConti.l4:'0'}	
			</c:set>
			
				
			<c:if test="${mov_codice!=g1_codice}">
			
				<c:if test="${livello!=4}" > 
					<% 
						//cerco il Mastro 
						String sql =null;
						try{ 
							sql = 	"select descrizione,codice from auser.PianoDeiConti p  " + 
							" where p.codice='" +  pageContext.getAttribute("g1_codice").toString() + "'" ; 
							results = com.spaceprogram.db4o.sql.Sql4o.execute(sfacade.getContainer(), sql);  
							pageContext.setAttribute( "mastroDesc", results.get(0).getObject("descrizione") );	
							pageContext.setAttribute( "mastroCod", results.get(0).getObject("codice") );	
						}catch(Exception e){
							pageContext.setAttribute("mastroDesc","");	
							pageContext.setAttribute("mastroCod","");	 
						}
						
					%>
				</c:if>
							 
				<tr style="display:${empty g1.codice?'none':''}">
					<td colspan="1">${livello==4?g1.codice:mastroCod}  
					<td colspan="1" >${livello==4?g1.vocePianoDeiConti.descrizione:mastroDesc}  
					<td colspan="1" align="right">&nbsp; 
						
						<c:if test="${totg1dare>0}"> 
							<fmt:formatNumber pattern='#,###.00' value='${totg1dare}' />
						</c:if> 
					<td colspan="1" align="right">&nbsp;
						<c:if test="${totg1avere>0}"> 
							<fmt:formatNumber pattern='#,###.00' value='${totg1avere}' /> 
						</c:if> 
				<c:set var="totg1dare" value="${0}" />
				<c:set var="totg1avere" value="${0}" />
						
			</c:if>		
			
			<c:if test="${mov.quadro=='d'}">
				<c:set var="totdare" value="${totdare+mov.importo}" />
				<c:set var="totg1dare" value="${totg1dare+mov.importo}" /> 
			</c:if>
			
			<c:if test="${mov.quadro=='a'}">
				<c:set var="totg1avere" value="${totg1avere+mov.importo}" />
				<c:set var="totavere" value="${totavere+mov.importo}" />
			</c:if>
			
			<c:set var="g1" value="${mov}" />
		</c:forEach>
		
		<c:set var="mov_codice" >
			${mov.vocePianoDeiConti.l1}.${livello>1?mov.vocePianoDeiConti.l2:'0'}.${livello>2?mov.vocePianoDeiConti.l3:'0'}.${livello>3?mov.vocePianoDeiConti.l4:'0'}
		</c:set> 
		<c:set var="g1_codice" >
			${g1.vocePianoDeiConti.l1}.${livello>1?g1.vocePianoDeiConti.l2:'0'}.${livello>2?g1.vocePianoDeiConti.l3:'0'}.${livello>3?g1.vocePianoDeiConti.l4:'0'}	
		</c:set>
		
		<c:if test="${livello!=4}" > 
			<% 
				//cerco il Mastro 
				try{ 
					String sql = 	"select descrizione,codice from auser.PianoDeiConti p  " + 
					" where p.codice='" +  pageContext.getAttribute("g1_codice").toString() + "'" ; 
					results = com.spaceprogram.db4o.sql.Sql4o.execute(sfacade.getContainer(), sql);  
					pageContext.setAttribute( "mastroDesc", results.get(0).getObject("descrizione") );	
					pageContext.setAttribute( "mastroCod", results.get(0).getObject("codice") );	
				}catch(Exception e){
					pageContext.setAttribute("mastroDesc","");	
					pageContext.setAttribute("mastroCod","");	 
				}
				
			%>
		</c:if>
		
		
		<tr style="display:${empty g1.codice?'none':''}">
			<td colspan="1">${livello==4?g1.codice:mastroCod}  
			<td colspan="1" >${livello==4?g1.vocePianoDeiConti.descrizione:mastroDesc}   
			<td colspan="1" align="right">&nbsp; 
				
				<c:if test="${totg1dare>0}"> 
					<fmt:formatNumber pattern='#,###.00' value='${totg1dare}' />
				</c:if> 
			<td colspan="1" align="right">&nbsp;
				<c:if test="${totg1avere>0}"> 
					<fmt:formatNumber pattern='#,###.00' value='${totg1avere}' /> 
				</c:if> 
		
		<c:if test="${st.count==1}">
			<c:set var="totpatdare" value="${totdare}"/>
			<c:set var="totpatavere" value="${totavere}" />
			<tr><td>99.AU.99.0<td>Riporto Perdita o Utile degli Esercizi<td id="ripdare" align="right">&nbsp;<td id="ripavere" align="right">&nbsp;
		</c:if>
		<c:if test="${st.count==2}">
			<c:set var="diff_ad" value="${totavere-totdare}" />
			
			<c:set var="avanzo" value="${diff_ad>0?diff_ad:0}" />
			<c:set var="disavanzo" value="${diff_ad<0?diff_ad*-1:0}" />
			
			<tr>
				<td>&nbsp;
				<td>Avanzo
				<td align="right">&nbsp; 
					
					<c:if test="${avanzo>0}"> 
						<fmt:formatNumber pattern='#,###.00' value='${avanzo}' />
					</c:if> 
				<td colspan="1" align="right" style="background-color:#e1e1e1">&nbsp;
					 
			
			<tr>
				<td>&nbsp;
				<td>Disavanzo
				<td colspan="1" align="right" style="background-color:#e1e1e1">&nbsp;
					
					 
				<td colspan="1" align="right">&nbsp;
					<c:if test="${disavanzo>0}"> 
						<fmt:formatNumber pattern='#,###.00' value='${disavanzo}' /> 
					</c:if> 
			
		</c:if>
		
		
		<tr style="font-weight:bold">
			<td colspan="2" style="border-width: 1px 1px 0 0">Totali di verifica ${st.first?vocedelpatrimoniale:vocedelleconomico}
			<td colspan="1" align="right" id="${st.first?'p':'e'}totdare">&nbsp; 
				
				<c:if test="${totdare>0}"> 
					<fmt:formatNumber pattern='#,###.00' value='${totdare+avanzo}' />
				</c:if> 
			<td colspan="1" align="right" id="${st.first?'p':'e'}totavere">&nbsp;
				<c:if test="${totavere>0}"> 
					<fmt:formatNumber pattern='#,###.00' value='${totavere+disavanzo}' /> 
				</c:if> 
				
		 
				
	</tbody>	
</table>	
  
 ${st.count==1?'<div class="break" style="margin-bottom:3px	"></div>':""}

</c:forEach>
<script>
	$(function(){
		if(${disavanzo}>0) $("#ripdare").html("<fmt:formatNumber pattern='#,###.00' value='${disavanzo}' />")
		if(${avanzo}>0) $("#ripavere").html("<fmt:formatNumber pattern='#,###.00' value='${avanzo}' />")
		
		$("#ptotdare").html("<fmt:formatNumber pattern='#,###.00' value='${disavanzo+totpatdare}' />");
		$("#ptotavere").html("<fmt:formatNumber pattern='#,###.00' value='${avanzo+totpatavere}' />");
	})
</script>

</body>
</html>